package com.aidong.carnival.controllers.mine;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aidong.activity.entity.MstCampaign;
import com.aidong.carnival.controllers.ActivitiesController;
import com.aidong.carnival.controllers.concerns.Failure;
import com.aidong.carnival.controllers.concerns.Result;
import com.aidong.carnival.controllers.concerns.Success;
import com.aidong.carnival.controllers.concerns.View;
import com.aidong.carnival.controllers.concerns.View.Base;
import com.aidong.carnival.entity.Message;
import com.aidong.carnival.entity.TrnInvitationCode;
import com.aidong.carnival.service.InvitationService;
import com.aidong.jobs.async.SmsTask;
import com.aidong.jobs.constants.JobConst;
import com.aidong.util.RandomUtil;
import com.fasterxml.jackson.annotation.JsonView;

@RestController
@RequestMapping("/mine")
public class InvitationController {
    @Autowired
    InvitationService invitationService;

    @Autowired
    private SmsTask smsTask;

    /**
     * 检查验证码和邀请码是否正确
     *
     * @param phone
     * @param invitationCode
     * @param code
     * @return
     */
    @JsonView(Base.class)
    @RequestMapping("/checkInvitation")
    public Result checkInvitation(@RequestParam("phone") String phone,
                                  @RequestParam("invitationCode") String invitationCode,
                                  @RequestParam("code") String code) {
        if (phone.trim().length() != 11) {
            return new Failure("手机号码输入有误");
        }
        phone = phone.trim();
        String[] msgCode = JobConst.msgCodeMap.get(phone);

        if (msgCode == null) {
            return new Failure("验证码已过期");
        }
        if (!msgCode[0].equals(code)) {
            return new Failure("验证码输入有误");
        }
        String result = invitationService.checkInvitation();
        if (StringUtils.isBlank(result)) {
            return new Failure("此邀请码无效");
        }
        String[] codes = result.split(",");
        boolean resu = true;
        for (String co : codes) {
            if (invitationCode.equalsIgnoreCase(co)) {
                resu = false;
                break;
            }
        }
        if (resu) {
            return new Failure("此邀请码无效");
        }

        TrnInvitationCode invitation = new TrnInvitationCode();
        invitation.setPhone(phone);
        invitation.setCode(invitationCode);
        MstCampaign campaign = invitationService.getCampaignDetailByType();
        if (campaign == null)
            return new Failure("暂无活动信息");
        int count = invitationService.getInvitationCounts(invitationCode.toLowerCase(), campaign.getCampaignId(), phone);

        if (count > 0) {
            int cou = invitationService.getInvitationByPhone(invitationCode, phone);
            if (cou > 0) {
                int camp = invitationService.getCampaignByPhone(phone, campaign.getCampaignId());
                if (camp > 0) {
                    return new Failure("您已报名该活动，请到订单列表查看");
                } else {
                    JobConst.msgCodeMap.remove(phone);
                    return new Success(new ModelMap());
                }
            } else {
                return new Failure("邀请码已使用或该手机号已绑定其他邀请码");
            }
        }
        invitation.setCampaignId(campaign.getCampaignId());
        JobConst.msgCodeMap.remove(phone);
        invitationService.addInvitation(invitation);
        return new Success(new ModelMap());
    }
    /**
     * 发送短信
     *
     * @param phone
     * @return
     */
    @JsonView(View.Base.class)
    @RequestMapping("/sendMsgCode")
    @ResponseBody
    public Result sendMsgCode(@RequestParam(value = "phone", required = true) String phone) {
        try {
            if (!StringUtils.isBlank(phone)) {
                if (phone.trim().length() != 11) {
                    return new Failure("手机号码输入有误");
                }
                phone = phone.trim();
                String captcha = RandomUtil.randomNumber(6);
                int code = Integer.parseInt(captcha);
                String[] codeAry = new String[2];
                codeAry[0] = String.valueOf(code);
                codeAry[1] = String.valueOf(System.currentTimeMillis());
                JobConst.msgCodeMap.put(phone, codeAry);
                smsTask.captcha(phone, String.valueOf(code));
                return new Success(new ModelMap());
            } else {
                return new Failure("参数缺失");
            }
        } catch (Exception e) {
            return new Failure("系统异常");
        }
    }
}
